package com.yjf.ljr.activity;

import com.yjf.ljr.R;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class EmailDeatailActivity extends IjActivity implements OnClickListener {
	@ID("rlchangeEmail")
	private RelativeLayout mRlchangeEmail;
	@ID("tveMail")
	private TextView mTvEmail;
	@ID("btn_title_left")
	private Button mBtTitleLeft;
	private String mail;// 电子邮箱
	private String mobile;// 手机号码 不展示
	private String viewMobile;// 手机号码 展示用
	private static final int CHANGE_MAIL = 1005;
	private static boolean RETURN_FLAG = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_email_deatail);
		initControl();
	}

	private void initControl() {
		setLeftBtnClick();
		setTitleStr("电子邮箱");
		mail = getIntent().getStringExtra("mail");
		mobile = getIntent().getStringExtra("mobile");
		viewMobile = getIntent().getStringExtra("viewMobile");
		if (!TextUtils.isEmpty(mail)) {
			mTvEmail.setText(mail);
		} else {
			mTvEmail.setText("暂无");

		}

		mRlchangeEmail.setOnClickListener(this);
		mBtTitleLeft.setOnClickListener(this);

	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.rlchangeEmail:
			Intent intent = new Intent(mContext, UpdateEmailActivity.class);
			intent.putExtra("mobile", mobile);
			intent.putExtra("viewMobile", viewMobile);
			// startActivity(intent);
			startActivityForResult(intent, CHANGE_MAIL);
			break;
		case R.id.btn_title_left:
			if (RETURN_FLAG) {
				RETURN_FLAG = false;
				Intent intentbtn = new Intent();
				intentbtn.putExtra("mailFlaf", true);
				setResult(RESULT_OK, intentbtn);
				finish();
			} else {
				EmailDeatailActivity.this.finish();
			}
			break;

		default:
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			if (RETURN_FLAG) {
				RETURN_FLAG = false;
				Intent intent = new Intent();
				intent.putExtra("mailFlaf", true);
				setResult(RESULT_OK, intent);
				finish();
				return true;
			}
			break;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, intent);
		if (requestCode == CHANGE_MAIL && resultCode == -1) {
			RETURN_FLAG = intent.getBooleanExtra("changemail", false);
			if (requestCode == CHANGE_MAIL && resultCode == -1 && RETURN_FLAG) {
				String newMial = intent.getStringExtra("newemail");
				if (!TextUtils.isEmpty(newMial)) {
					mTvEmail.setText(newMial);
				} else {
					mTvEmail.setText("暂无");
				}
			}
		}
	}

}
